package com.moonfish.base.controller;


import com.moonfish.base.security.JwtTokenUtil;
import com.moonfish.base.security.JwtUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.userdetails.UserDetailsService;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by mayu on 2017/7/11.
 * email:showmara@qq.com
 */
public class BaseController {
    @Value("${jwt.header}")
    private String tokenHeader;
    private final JwtTokenUtil jwtTokenUtil;

    private final UserDetailsService userDetailsService;

    public BaseController(JwtTokenUtil jwtTokenUtil, UserDetailsService userDetailsService)
    {
        this.jwtTokenUtil = jwtTokenUtil;
        this.userDetailsService = userDetailsService;
    }
    public JwtUser getAuthenticatedUser(HttpServletRequest request) {
        String token = request.getHeader(tokenHeader);
        String username = jwtTokenUtil.getUsernameFromToken(token);
        return (JwtUser) userDetailsService.loadUserByUsername(username);
    }
}
